lib.use(['element', 'table', 'form','laydate','tablePlug','notice'], function () {
    var notice=layui.notice;
    var table = layui.table;
    var layer = layui.layer;
    var laydate = layui.laydate;
    var form = layui.form;
    var tranHeadTable;
    var searchFrm = layui.Form('searchFrm');

    tablePlug = layui.tablePlug //表格插件

    table.set(common.layui.table.config(tablePlug));

    searchFrm.selectRender();

    laydate.render({elem: '#tranDate' ,range: '至'});
    laydate.render({elem: '#checkDate' ,range: '至'});

    // 渲染表格
    tranHeadTable = table.render({
        elem: '#tranHeadTable'
        , url: common.util.createUrl('/inv/tranHead/list')
        // 对分页请求的参数：page、limit重新设定名称
        , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        , height: 'full-80'
        , cols: [[
             {type:'checkbox'}
            ,{type: 'numbers', title: '编号' }
            ,{ field: 'id',  hide: true }
            ,{ field: 'entId',  hide: true}
            ,{ field: 'id',  hide: true}
            ,{ title: '调拨编码', field: 'tranNo' }
            ,{ title: '调拨日期', field: 'tranDate' }
            ,{  field: 'toWarehouseId',  hide: true}
            ,{ title: '调入仓库编码', field: 'toWarehouseCode' }
            ,{ title: '调入仓库名称', field: 'toWarehouseName' }
            ,{ field: 'fromWarehouseId',  hide: true}
            ,{ title: '调出仓库编码', field: 'fromWarehouseCode' }
            ,{ title: '调出仓库名称', field: 'fromWarehouseName' }
            ,{ title: '单据状态', field: 'tranState' ,templet: function (row) { return common.dist.getValue("inv.TranHead_TranStateEnum",row.tranState);}}
            ,{ field: 'checkUserId',  hide: true}
            ,{ field: 'checkUserName',  hide: true}
            ,{ title: '审核时间', field: 'checkDate' }
            ,{ title: '调拨类型', field: 'tranType' ,templet: function (row) { return common.dist.getValue("inv.TranHead_TranTypeEnum",row.tranType);}}
            ,{ field: 'remark',  hide: true}
            /*,{ title: '创建人id', field: 'createUserId',  hide: true}
            ,{ title: '创建人', field: 'createUserName',  hide: true}*/
            ,{ title: '创建时间', field: 'createTime',  hide: true}
           /* ,{ title: '修改人id', field: 'updateUserId',  hide: true}
            ,{ title: '修改人', field: 'updateUserName' }
            ,{ title: '修改时间', field: 'updateTime' }*/
        ]]
       /* ,done: function () {
            layer.closeAll('loading');
        }*/
    });
    //监听行单击事件（单击事件为：rowDouble）
    table.on('rowDouble(tranHeadTableFilter)', function(obj){
        update(obj)
    });
    $(document).on('click','#del',function(){
        del(table.checkStatus('tranHeadTable'))
    });
    $(document).on('click','#check',function(){
        check(table.checkStatus('tranHeadTable'))
    });
    $(document).on('click','#add',function(){
        var config = {
            title:"新增调拨单",
            content: 'inv/tranHead/tranHeadForm.html'
        }
        common.util.openWindow(busCommon.windowConfig(config))
    });
    $(document).on('click','#export',function(){
        var params=common.from.serializeObject('#searchFrm');
        params['pages']=tranHeadTable.config.page.pages;
        params['limit']=tranHeadTable.config.page.limit;
        common.util.exportWindows('/inv/tranHead/export',params)
    });

    form.on('submit(searchFilter)', function(data){
        var param = data.field;
        searchTable(param)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
        params=common.from.serializeObject('#searchFrm');
        tablePlug.getIns(tranHeadTable.config.id).reload({where:params});

    }
    /**
     * 修改
     * @param obj
     */
    function update(obj) {
        if(!common.auth.hasPermission("inv:outHead:update")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length!=1)return;
        var config = {
            title:"修改调拨单",
            content: 'inv/tranHead/tranHeadForm.html?id=' + postData[0]
        }
        common.util.openWindow(busCommon.windowConfig(config))
    }
    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        if(!common.auth.hasPermission("inv:tranHead:delete")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认删除!', function(index){
            console.log('Config Del param is', postData);
            common.ajax.get('/inv/tranHead/delete/'+postData[0],null, function (resp) {
                searchTable();
                layer.close(index);
            })

        });
    }

    /**
     * 审核
     * @param obj
     */
    function check(obj) {
        if(!common.auth.hasPermission("inv:tranHead:check")){return;}
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }

        if(postData.length!=1){common.util.error('请选择一条记录进行操作');return;}
        common.util.confirm('确认审核!', function(index){
            console.log('Config Check param is', postData);
            common.ajax.get('/inv/tranHead/check/'+postData[0], null, function (resp) {
                searchTable();
                layer.close(index);
                common.util.success("审核成功")
            })

        });
    }
    window.formCallBack=function (params) {
        searchTable(params);
    };
});
